$(function() {
    {if $Think.get.order_start||$Think.get.order_end||$Think.get.year}
    $('#showmore').css('display','block')
    {/if}
    var show = 0
    var navulwid = 0;

    console.log($('#scrollmain').width())
    for(var wid = 0; wid < $(".amnav>ul li").length; wid++) {
        navulwid += $(".amnav>ul li").eq(wid).width()
    }
    console.log($('#scrollmain').width() < navulwid)
    if($('#scrollmain').width() < navulwid) {

        $(".amnav>ul").width(navulwid+100);
        var orderScrollnav = new IScroll('.amnav', {
            mouseWheel: true,
            scrollX: true,
            click: true
        });
    }
    $('.firstcheck').on('click',function(){
        var that=this
        var title=''
        if($(that).data('checklevel')==3){}else{
            if($(that).data('checklevel')==1){
                title='请选择复审人员'
            }else if($(that).data('checklevel')==2){
                title='请选择终审人员'
            }
            var choose=layer.open({
                type: 1,
                title:title,
                skin: 'layui-layer-rim', //加上边框
                area: ['300px', '240px'], //宽高
                content: `
		  	<div class="am-u-sm-12" style='padding:10px'>
		  		<input style='width:100%;height:30px' type="text"/>
		  	</div>
		  	<div class="am-u-sm-12" style='padding:10px'>
		  		<button class='am-btn am-btn-sm am-btn-primary am-radius chooseappover'>确定</button>
		  		<button class='am-btn am-btn-sm am-btn-primary am-radius chooseno'>取消</button>
		  	</div>`
                ,
                success:function(){
                    $('.chooseappover').on('click',function(){
                        if($(that).data('checklevel')==1){
                            $(that).html('复审')
                            $(that).data('checklevel',2)
                        }else if($(that).data('checklevel')==2){
                            $(that).html('终审')
                            $(that).data('checklevel',3)
                        }
                        layer.close(choose)
                    })
                    $('.chooseno').on('click',function(){
                        layer.close(choose)
                    })

                }
            });
        }

    })

    //免单 BY 发仔 2018-08-27
    $('.setFree').click(function () {
        let tr = $(this).closest('tr');
        let oid = tr.data('id');
        layer.confirm('是否确认免单？',function () {
                // alert(oid);
                $.post("{:url('setFree')}",{oid:oid},function (res) {
                    if(res.code) {
                        Msg('成功免单');
                        tr.remove();
                    } else {
                        Err('免单失败，请检查该订单是否满足免单条件');
                    }
                });
            }
        );
    });

    //管理员代支付 2018-08-27 BY 发仔
    $('.adminOrderPay').click(function () {
        let tr = $(this).closest('tr');
        let oid = tr.data('id');
        layer.confirm('是否确认支付？',function () {
                // alert(oid);
                $.post("{:url('adminOrderPay')}",{oid:oid},function (res) {
                    if(res.code) {
                        Msg('成功支付');
                        tr.remove();
                    } else {
                        Err('支付失败，请检查该订单是否满足支付条件');
                    }
                });
            }
        );
    });

    $('.check').on('click',function(){
        var that=this
        var auto = layer.open({
            type: 1,
            title:false,
            skin: 'layui-layer-rim', //加上边框
            area: ['200px', '100px'], //宽高
            content: `
   			<div class='am-u-sm-12' style='padding:10px'>
   				<div class='am-u-sm-12' style='text-align:center;margin-bottom:10px'>是否确认该订单</div>
   				<div class='am-u-sm-12' style='text-align:center'>
   					<a class='am-btn am-btn-sm am-btn-primary am-radius close_ok'>审批</a>
   					<a class='am-btn am-btn-sm am-btn-primary am-radius close_no'>取消</a>
   				</div>
   			</div>
   		`,
            success:function(){

                $('.close_ok').on('click',function(){
                    var id=$(that).closest('tr').data('id')
                    Load()
                    $.ajax({
                        type:'post',
                        data:{
                            order_id:id,
                            order_state:2
                        },
                        url:'{:url("edit")}',
                        success:function(res){
                            Load(1)
                            if(res.code==1){
                                Msg('审批成功');
                                window.history.go(0)
                            }else{
                                Err(res.msg)
                            }
                        },
                        error:function(){
                            Load(1)
                            Err('请求失败')
                        }
                    })
                    layer.close(auto)
                });
                $('.close_no').on('click',function(){
                    layer.close(auto)
                })
            }
        })
    })

    $('.company_name').on('click', function() {
        choosecar("{:url('company/index',['op'=>'search'])}")
    });
    $("#more").on("click", function() {
        if(show == 0) {

            $('#showmore').css('display', 'block')
            show = 1
        } else {
            $('#showmore').css('display', 'none')
            show = 0
        }
    })
    $(".showmap").on('click', function() {
        popup(this)
        var that = this
        if($(this).data('method') == "showmap") {
            var trackload = layer.load(1, {
                shade: [0.1, '#fff'] //0.1透明度的白色背景
            });
            var map = new AMap.Map('showlocation', {
                resizeEnable: true,
                zoom: 8,
                center: [118.57, 24.82]
            });
            AMapUI.load(['ui/misc/PathSimplifier'], function(PathSimplifier) {
                var pathSimplifierIns = new PathSimplifier({
                    zIndex: 100,
                    map: map, //所属的地图实例
                    getPath: function(pathData, pathIndex) {
                        //返回轨迹数据中的节点坐标信息，[AMap.LngLat, AMap.LngLat...] 或者 [[lng|number,lat|number],...]
                        return pathData.path;
                    },
                    getHoverTitle: function(pathData, pathIndex, pointIndex) {
                        //返回鼠标悬停时显示的信息
                        if(pointIndex >= 0) {
                            //鼠标悬停在某个轨迹节点上
                            return pathData.name + '，点:' + pointIndex + '/' + pathData.path.length;
                        }
                        //鼠标悬停在节点之间的连线上
                        return pathData.name + '，点数量' + pathData.path.length;
                    },
                    renderOptions: {
                        //轨迹线的样式
                        pathLineStyle: {
                            strokeStyle: 'red',
                            lineWidth: 6,
                            dirArrowStyle: true
                        }
                    }
                });
                $.ajax({
                    type: "post",
                    url: "{:url('getTrack')}",
                    data: {
                        oid: $(that).closest('tr').data('id')
                    },
                    success: function(res) {
                        layer.close(trackload);
                        var allpath = []
                        var speed=0;
                        if(typeof res == 'string') {
                            var opath1 = JSON.parse(res).data;
                        } else {
                            var opath1 = res.data;
                        }
                        var opath2=[];
                        var opath=[];
                        //根据时间进行排序
                        if(opath1.length>1){
                            for(var rankidx=0;rankidx<opath1.length;rankidx++){
                                opath2.push(opath1[rankidx].time)
                            }
                            opath2.sort()
                            for(var k=0;k<opath2.length;k++){
                                for(var j=0;j<opath1.length;j++){
                                    if(opath2[k]==opath1[j].time){
                                        opath.push(opath1[j])
                                    }
                                }
                            }
                            console.log(opath)

                            for(var oi = 0; oi < opath.length; oi++) {
                                var oarr = []
                                oarr.push(opath[oi].longitude)
                                oarr.push(opath[oi].latitude)
                                allpath.push(oarr)
                            }
                            initPage(PathSimplifier, pathSimplifierIns, map, allpath)
                        }else{
                            Err('暂无轨迹信息')
                        }
                    },
                    error:function(){
                        layer.close(carload);
                        Err('请求失败')
                    }
                });
            })
        }
    });
    $('.car_type').on('click', function() {
        choosecar("{:url('models/index',['op'=>'search'])}")
    });
    $('.cancel').on('click', function() {
        popup(this, "{:url('state')}");
    });
    $('.consent').on('click', function() {
        popup(this, "{:url('state')}");
    });
    $('.repulse').on('click', function() {
        popup(this, "{:url('state')}");
    });
    $('.am-nav-tabs li a').on('click', function() {
        var index = layer.load(1, {
            shade: [0.1, '#fff'] //0.1透明度的白色背景
        });
    });
    $('.order_start').on('change',function(){
        datejudge($('.order_start'), $('.order_end'))
    })
    $('.order_end').on('change',function(){
        datejudge($('.order_start'), $('.order_end'))
    })
    function initPage(PathSimplifier, pathSimplifierIns, map, path) {
        var alljourney=0
        for(var pidx=0;pidx<path.length-1;pidx++){
            var  journey=Math.sqrt(Math.pow((path[pidx][0]-path[pidx+1][0]),2)+Math.pow((path[pidx][1]-path[pidx+1][1]),2))*111000
            alljourney+=journey
        }

        console.log(alljourney)
        //这里构建两条简单的轨迹，仅作示例
        pathSimplifierIns.setData([{
            name: '轨迹',
            path: path
        }]);

        //创建一个巡航器
        var navg0 = pathSimplifierIns.createPathNavigator(0, //关联第1条轨迹
            {
                loop: true, //循环播放
                speed:alljourney/10,
                pathNavigatorStyle: {
                    width: 16,
                    height: 32,
                    content: PathSimplifier.Render.Canvas.getImageContent('{$static_dir}/assets/img/car-3.png', onload, onerror),
                    strokeStyle: null,
                    fillStyle: null
                }
            });

        navg0.start();
    }

})